<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\MoneyLog;
use app\common\model\order\Order;
use app\common\model\User;

/**
 * 配送员接口
 */
class Deliveryman extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (配送员数据)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function index()
    {
        $take_num = Order::where('take_id', $this->auth->id)->whereTime('taketime', 'month')->whereNotIn('order_status', ['0', '-1'])->count();
        $send_num = Order::where('send_id', $this->auth->id)->whereTime('cleantime', 'month')->whereNotIn('order_status', ['0', '-1'])->count();
        $month_num = $take_num + $send_num;
        $total_take_num = Order::where('take_id', $this->auth->id)->whereNotIn('order_status', ['0', '-1'])->count();
        $total_send_num = Order::where('send_id', $this->auth->id)->whereNotIn('order_status', ['0', '-1'])->count();
        $total_num = $total_take_num + $total_send_num;
        $this->success('请求成功', compact('month_num', 'total_num'));
    }

    /**
     * @Author LK
     * @ApiTitle (配送员列表)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $user = User::all(['group_id' => 3]);
        $this->success('请求成功', $user);
    }
    /**
     * @Author LK
     * @ApiTitle (配送员订单)
     * @ApiMethod (get)
     * @ApiParams (name="order_type", type="string", required=true, sample="", description="订单类型：1=取鞋订单，2=配送订单")
     * @ApiParams (name="order_status", type="integer", required=true, sample="", description="all=全部；10=待取鞋,20=已取鞋,30=待配送,40=配送中,50=已完成")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function shop_order()
    {
        $param = request()->param();
        extract($param);
        $order = new Order;
        if ($order_type == 1) {
            if ($order_status != 'all') {
                if ($order_status == '10') {
                    $order->where('order_status', $order_status);
                } else {
                    $order->where('order_status', '>', '10');
                }
            }

            $order->where('take_id', $this->auth->id);
        } else {
            if ($order_status != 'all') {
                if ($order_status == '50') {
                    $order->whereIn('order_status', ['50', '60']);
                } else {
                    $order->where('order_status', $order_status);
                }
            }
            $order->where('send_id', $this->auth->id);
        }
        $list = $order->where('order_status', '<>', '-1')->order('id desc')->paginate();
        $this->success('请求成功', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (配送员操作)
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="take_code", type="integer", required=true, sample="", description="取鞋码")
     * @ApiParams (name="take_images", type="integer", required=true, sample="", description="取鞋图片")
     * @ApiParams (name="status", type="integer", required=true, sample="", description="10=取鞋,20=开始配送")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function controls()
    {
        $param = request()->param();
        extract($param);
        $time = time();
        $order = Order::get($order_id);
        if (!$order) {
            $this->error('订单不存在');
        }
        if ($status == 10) {
            if ($take_code != $order->take_code) {
                $this->error('取鞋码错误');
            }
            $order->taketime = $time;
            $order->take_images = $take_images;
            $order->order_status = '15';
        } elseif ($status == 20) {
            $order->order_status = '40';
        } elseif ($status == 30) {
            MoneyLog::subcommission($order->user_id, $order->orderid);
            $order->order_status = '50';
            $order->sendtime = $time;
        }

        $res = $order->save();
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
}
